$(function(){

	function isMobile(value)
	{
		var mobile = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(14[0-9]{1}))+\d{8})$/;
		return (value.length == 11 && mobile.test(value));
	}

	function codeCountDown(obj, str)
	{
		var index = 60;
		obj.addClass('disabled');
		obj.html('剩余' + index + '秒');

		var timer = setInterval(function(){
			obj.html('剩余' + index + '秒');

			if (index == 0)
			{
				clearInterval(timer);
				obj.removeClass('disabled');
				obj.html(str);
			}

			index --;



		}, 1000);


	}
	$('#btn_refresh_authnum').click(function(){
		alert('path='+path);
		$('#authnum_image').attr('src', path+'validateCode?time='+new Date().getTime());

	});
	function forgotSubmitHandler(form)
	{
		$('button[type="submit"]', form).prop('disabled', true);
		$('button[type="submit"] span', form).html('发送请求中...');
		var options = {
			dataType: 'json',
			url: $(form).attr('data-url'),
			success: function(res){
				if (res.code === '1'){
					common_show_success('找回密码链接已成功发送到指定邮箱');
					setTimeout(function(){
						location.href = path+'/login.action';
					}, 2000);

				}
				else{
					$('.alert-error span',form).html(res.message);
					$('.alert-error', form).fadeIn();
					$('button[type="submit"] span', form).html('发送请求');
					$('button[type="submit"]', form).prop('disabled', false);

				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				$('button[type="submit"] span', form).html('发送请求');
				$('button[type="submit"]', form).prop('disabled', false);
			}
		}

		form.ajaxSubmit(options);
	}

	function loginSubmitHandler(form)
	{
		$('button[type="submit"]', form).prop('disabled', true);
		$('button[type="submit"] span', form).html('登陆中...');
		var pass = $('input[name="password"]', form);
		var passValue = pass.val();
		//pass.val(calcMD5(passValue));
		var optionsform = {
			//dataType: 'json',
			url: $(form).attr('data-url'),
			success: function(data){
				var dataArr = data.split("$");
				if (dataArr[1]=="LOGIN_USER_SUCCESS"){

					var expires_value = 3; //默认3天
					/*
					 if ($('#remember_checkbox').is(':checked'))
					 {
					 expires_value = 3;
					 }
					 */

					//$.cookie('member_cookie', res.data, { expires: expires_value, path: '/'}); //设置COOKIE

					location.href = '/login/show';

				}
				else{

					$('.alert-error span',form).html(initLoginMsg(dataArr[1]));
					//$('.alert-error span',form).html(data.message);
					$('.alert-error', form).fadeIn();
					$('input[name="password"]', form).val('');
					$('input[name="user_identify"]', form).val('');
					$('button[type="submit"] span', form).html('提交登陆');
					$('button[type="submit"]', form).prop('disabled', false);
					//$('#authnum_image').attr('src', path+'get?time='+new Date().getTime());
					$('#authnum_image').attr("src", "/code/get?" + Math.random() * 100 + (new Date()).valueOf());
					$('#loginsubmit').attr("disabled",true);
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				$('button[type="submit"] span', form).html('提交登陆');
				$('button[type="submit"]', form).prop('disabled', false);
			}
		}

		form.ajaxSubmit(optionsform);
	}

	function registerSubmitHandler(form)
	{
		$('button[type="submit"]', form).prop('disabled', true);
		$('button[type="submit"] span', form).html('提交注册中...');
		var password = $('input[name="password"]', form);
		password.val(calcMD5(password.val()));
		var repassword = $('input[name="repassword"]', form);
		repassword.val(calcMD5(repassword.val()));
		var options = {
			dataType: 'json',
			url: $(form).attr('data-url'),
			success: function(res){
				if (res.code === '1'){

					var tpl = $('<div class="alert alert-success" style="position:fixed; top: 0; left: 50%; margin-left: -280px;  width: 560px; z-index: 9999;"><i class="ace-icon fa fa-check"></i>恭喜，注册成功！2秒后自动登陆...</div>');
					$('body').append(tpl);
					setTimeout(function(){
						tpl.animate({opacity: 0, top: '-35px'}, 1000, function(){
							$(this).remove();
						});
					}, 1200);

					setTimeout(function(){
//						var expires_value = 3; //默认3天
//						$.cookie('member_cookie', res.data, { expires: expires_value, path: '/'}); //设置COOKIE
						location.href = path+'/membersCenter.action';
					}, 2000);
				}
				else{
					$('.alert-error span',form).html(res.message);
					$('.alert-error', form).fadeIn();
					$('input[name="password"]', form).val('');
					$('input[name="repassword"]', form).val('');
					$('button[type="submit"] span', form).html('提交注册信息');
					$('button[type="submit"]', form).prop('disabled', false);
					$('#btn_refresh_authnum').trigger('click');

				}
			},
			error: function(){
				$('button[type="submit"] span', form).html('提交注册信息');
				$('button[type="submit"]', form).prop('disabled', false);
			}
		}

		form.ajaxSubmit(options);
	}


	$('#btn_forgot').click(function(){
		$('#login_dialog').modal('hide');
		$('#forgot_dialog').modal('show');

		$('#forgot_form input').placeholder({isUseSpan: true});
	})
	//点击验证码
	$('#mobile_code').click(function(){

		var self = $(this),
				mobile = $('#mobile').val();

		self.prop('disabled', true);

		$('#mobile_code').html('获取中...');

		if (mobile && isMobile(mobile))
		{
			$.ajax({
				type: 'post',
				dataType: 'json',
				url: path+'/createAuthCode.action',
				data: {
					mobile: mobile
				},
				success: function(res)
				{
					self.prop('disabled', false);
					if (res.code == '1'){
						common_show_success('手机验证码发送成功');
					}else{
						common_show_error(res.message);
					}
					codeCountDown($('#mobile_code'), '获取验证码');
				},
				error: function(){
					self.prop('disabled', false);
					$('#mobile_code').html('获取验证码');
					common_show_error(res.message);
				}

			});

		}
		else
		{
			common_show_error('手机号格式错误，请重新输入');
			self.prop('disabled', false);
			$('#mobile_code').html('获取验证码');
		}

	});

	$('.radio-input-box .lbl').click(function(){
		var self = $(this);
		$('.radio-input-box .lbl i').removeClass('fa-dot-circle-o');
		self.find('i').addClass('fa-dot-circle-o');
		$('#reg_form [name="type"]').val(self.attr('data-type'));
	});

	$('#fullpage').fullpage({
		slidesColor: ['#fff', '#fff', '#fff', '#fff'],
		anchors: ['page1', 'page2',  'page4', 'page5'],
		menu: '#menu'
	});

	$('.btn_login').click(function(){
		$('#login_dialog').modal('show');

		$('#login_form input').placeholder({isUseSpan: true});
	});

	$('.btn_register').click(function(){
		$('#register_dialog').modal('show');
	});

	$('.product-list').ace_scroll({
		size: 500
	});

	$('.alert-error .close').click(function(){
		var self = $(this);
		self.parent().fadeOut();
	});

	$('#forgot_form').validate({
		errorElement: 'div',
		errorClass: 'help-block',
		focusInvalid: false,
		rules: {
			username: {
				required: true
			},
			email: {
				required: true,
				email: true

			}

		},

		messages: {
			username: {
				required: '请输入登陆名/账号'
			},
			email: {
				required: '请输入邮箱',
				email: '邮箱格式不正确'
			}
		},


		highlight: function (e) {
			$(e).closest('label').removeClass('has-info').addClass('has-error');
		},

		success: function (e) {
			$(e).closest('label').removeClass('has-error');//.addClass('has-info');
			$(e).remove();
		},

		submitHandler: function (form) {
			forgotSubmitHandler($(form));

			return false;
		},
		invalidHandler: function (form) {
		}
	});

	$('#login_form').validate({
		errorElement: 'div',
		errorClass: 'help-block',
		focusInvalid: false,
		rules: {
			username: {
				required: true
			},
			password: {
				required: true

			}

		},

		messages: {
			username: {
				required: '请输入登陆名/账号'
			},
			password: {
				required: '请输入登陆密码'
			}
		},


		highlight: function (e) {
			$(e).closest('label').removeClass('has-info').addClass('has-error');
		},

		success: function (e) {
			$(e).closest('label').removeClass('has-error');//.addClass('has-info');
			$(e).remove();
		},

		submitHandler: function (form) {
			loginSubmitHandler($(form));

			return false;
		},
		invalidHandler: function (form) {
		}
	});

	$('#reg_form').validate({
		errorElement: 'div',
		errorClass: 'help-block',
		focusInvalid: false,
		rules: {
			accept:{
				required:true
			},
			email: {
				required:true,
				email: true
			},
			username: {
				required: true,
				rangelength:[6,20],
				remote:{ //验证用户名是否存在
					type: 'POST',
					url: path+'/ifUsernameExist.action',
					data: {
						username: function(){
							return $('#reg_form [name="username"]').val();
						}
					}
				}
			},


			password: {
				required: true,
				isPassword: true
			},
			repassword:{
				equalTo: '#password'
			}

		},

		messages: {
			accept:{
				required:''
			},
			email: {
				required:'请输入邮箱',
				email: '邮箱格式不正确'
			},
			username: {
				required: '请输入用户名',
				rangelength: '用户名6-20个字符',
				remote: '用户名已注册'
			},
			password: {
				required: '请输入密码',
				isPassword: '密码应该是6-20个字符组成，不包含空格'
			},
			repassword:{
				equalTo: '两次输入密码不一致'
			}
		},


		highlight: function (e) {
			$(e).closest('label').removeClass('has-info').addClass('has-error');
		},

		success: function (e) {
			$(e).closest('label').removeClass('has-error');//.addClass('has-info');
			$(e).remove();
		},

		submitHandler: function (form) {
			registerSubmitHandler($(form));

			return false;
		},
		invalidHandler: function (form) {
		}
	});
	function initLoginMsg(msgtype){
		if(msgtype=="LOGIN_USER_PASSWORD_WRONG"){
			return "密码错误";
		}else if(msgtype=="LOGIN_USER_NOT_EXIST"){
			return "用户不存在";
		}else{
			return "用户名或密码错误";
		}

	}

});